Programowanie obiektowo-orientowane

  • Wszystko jest obiektem w Pythonie
  • Klasy, instancje
  • Brak interfejsów
  • Wielodziedziczenie
  • przeciążanie operatorów, magic methods np. (__contains__)
  • sprawdzanie typów przez type()

In [ ]:
class Point():
    def x(self, x):
        self.x = x
        
    def y(self, y):
        self.y = y
        
    def double(self):
        return self.x * self.y
    
p = Point()
p.y(5)
p.x(10)
print(p.double())

In [ ]:
class Point():
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def double(self):
        return self.x * self.y
    
p = Point(3, 4)
print(p.double())

In [ ]:
class Point():
    """This is a docstring"""
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def double(self):
        return self.x * self.y
    
Point?

In [ ]:
class Point():
    """This is a docstring"""
    
    version = "0.1"
    
    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version
        
    def double(self):
        return self.x * self.y
    
    def print_version(self):
        print(self.version)
    
print(Point.version)
Point(1, 2, version="1").print_version()

In [ ]:
class Point3D(Point):
    
    def __init__(self, x, y, z, version=None):
        self._z = z
        super().__init__(x, y, version)
        
p = Point3D(1, 2, 3, "1")
p.print_version()
print(p._z)

Python2

class Point(object):
    """This is a docstring"""

    version = "0.1"

    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version

    def double(self):
        return self.x * self.y

    def print_version(self):
        print(self.version)

Python2

class Point3D(Point):

    def __init__(self, x, y, z, version=None):
        self.z = z
        super(Point3D, self).__init__(x, y, version)

In [ ]:
class Point3D(Point):
    
    def __init__(self, x, y, z, version=None):
        self._z = z
        super().__init__(x, y, version)
        
    def double(self):
        return self._z * self.x * self.y
        
        
p = Point3D(1, 2, 3, "1")
print(p.double())

In [ ]:
class Point():
    """This is a docstring"""
    
    version = "0.1"
    
    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version
            
    @staticmethod
    def add(a, b):
        return Point(a.x + b.x, a.y + b.y)
        
    @classmethod    
    def fromstrings(cls, x, y):
        return cls(int(x), int(y))
            
        
    def double(self):
        return self.x * self.y
    
    def print_version(self):
        print(self.version)

In [ ]:
p = Point.fromstrings('1', '2')
print(p.double())
p2 = Point.add(p, p)
print(p2.x, p2.y)
print(p2.double())

In [ ]:
class Point():
    """This is a docstring"""
    
    __slots__ = ['x', 'y']
    
    version = "0.1"
    
    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version
            
    @staticmethod
    def add(a, b):
        return Point(a.x + b.x, a.y + b.y)
        
    @classmethod    
    def fromstrings(cls, x, y):
        return cls(int(x), int(y))
        
    def double(self):
        return self.x * self.y
    
    def print_version(self):
        print(self.version)

In [ ]:
p = Point.fromstrings('1', '2')
print(p.double())
p2 = Point.add(p, p)
print(p2.x, p2.y)
print(p2.double())

In [ ]:
class Square():
    
    def __init__(self, x):
        self.x = x
        
    def __repr__(self):
        return "Square(%d)" % self.x
    
    def __str__(self):
        return "[%d]" % self.x
    
print(repr(Square(5)))
print(str(Square(5)))